<template>
  <a-card :title="title" id="JTQBox">
    <a-descriptions size="small" :column="1" bordered title="中频解调接收分机状态" title-class-name="my-custom-title">
      <a-descriptions-item label="电压">
        {{ desData.dy ? desData.dy : '/' }}
      </a-descriptions-item>
      <a-descriptions-item label="电流">
        {{ desData.dl ? desData.dl : '/' }}
      </a-descriptions-item>
      <a-descriptions-item label="温度">
        {{ desData.wd ? desData.wd : '/' }}
      </a-descriptions-item>
      <!-- <a-descriptions-item label="解调状态">
        {{ desData.jtzt ? desData.jtzt : '/' }}
      </a-descriptions-item> -->
      <a-descriptions-item label="信号输入">
        {{ desData.xhsr ? desData.xhsr : 'L频段' }}
      </a-descriptions-item>
    </a-descriptions>

    <a-descriptions size="small" :column="2" bordered title="通道板参数" title-class-name="my-custom-title">
      <a-descriptions-item label="下行通道1(国内)衰减器1">
        {{ desData.xxtd1_sjq1_gn ? desData.xxtd1_sjq1_gn : '/' }}
      </a-descriptions-item>
      <a-descriptions-item label="下行通道1(国内)衰减器2">
        {{ desData.xxtd1_sjq2_gn ? desData.xxtd1_sjq2_gn : '/' }}
      </a-descriptions-item>
      <a-descriptions-item label="下行通道1(国际)衰减器1">
        {{ desData.xxtd1_sjq1_gj ? desData.xxtd1_sjq1_gj : '/' }}
      </a-descriptions-item>
      <a-descriptions-item label="下行通道1(国际)衰减器2">
        {{ desData.xxtd1_sjq2_gj ? desData.xxtd1_sjq2_gj : '/' }}
      </a-descriptions-item>
      <a-descriptions-item label="上行通道衰减器1">
        {{ desData.sxtd_sjq1 ? desData.sxtd_sjq1 : '/' }}
      </a-descriptions-item>
      <a-descriptions-item label="上行通道衰减器2">
        {{ desData.sxtd_sjq2 ? desData.sxtd_sjq2 : '/' }}
      </a-descriptions-item>
      <a-descriptions-item label="本振锁定指示">
        {{ desData.bzsdzs === '0' ? '本振失锁' : desData.bzsdzs === '1' ? '本振锁定' : '/' }}
      </a-descriptions-item>
      <a-descriptions-item label="设备状态">
        <!--            {{ desData.sbzt === '0' ? '正常' : desData.sbzt === '1' ? '故障' : "/" }}-->
        {{ desData.dl2 ? (desData.dl2 > 550 && desData.dl2 < 800 ? '正常' : '故障') : '/' }}
      </a-descriptions-item>
      <a-descriptions-item label="电流(mA)">
        {{ desData.dl2 ? desData.dl2 : '/' }}
      </a-descriptions-item>
      <a-descriptions-item label="温度">
        {{ desData.wd2 ? desData.wd2 : '/' }}
      </a-descriptions-item>
      <a-descriptions-item label="下行国内通道输入功率">
        {{ desData.xxgntdsrgl ? Number(desData.xxgntdsrgl) : '/' }}
        <!-- {{ desData.xxgntdsrgl ? Number(desData.xxgntdsrgl) / 100 : '/' }} -->
      </a-descriptions-item>
      <a-descriptions-item label="下行国际通道输入功率">
        {{ desData.xxgjtdsrgl ? Number(desData.xxgjtdsrgl) : '/' }}
        <!-- {{ desData.xxgjtdsrgl ? Number(desData.xxgjtdsrgl) / 100 : '/' }} -->
      </a-descriptions-item>
      <a-descriptions-item label="设备地址">
        {{ desData.sbdz ? desData.sbdz : '/' }}
      </a-descriptions-item>
      <a-descriptions-item label="ip地址">
        {{ desData.ip ? desData.ip : '/' }}
      </a-descriptions-item>
      <a-descriptions-item label="下行国内通道输入功率电压">
        {{ desData.xxgntdsrgldy ? Number(desData.xxgntdsrgldy) : '/' }}
        <!-- {{ desData.xxgntdsrgldy ? Number(desData.xxgntdsrgldy) / 100 : '/' }} -->
      </a-descriptions-item>
      <a-descriptions-item label="下行国际通道输入功率电压">
        {{ desData.xxgjtdsrgldy ? Number(desData.xxgjtdsrgldy) : '/' }}
        <!-- {{ desData.xxgjtdsrgldy ? Number(desData.xxgjtdsrgldy) / 100 : '/' }} -->
      </a-descriptions-item>
    </a-descriptions>

    <a-descriptions size="small" :column="2" bordered title="导频发射分机遥测状态" title-class-name="my-custom-title">
      <a-descriptions-item label="导频信号开关">
        {{ desData.dpxhkg === '0' ? '关' : desData.dpxhkg === '1' ? '开' : '/' }}
      </a-descriptions-item>
      <a-descriptions-item label="模式">
        {{ desData.dpgzms === '1' ? '工作模式' : desData.dpgzms === '2' ? '测试模式' : desData.dpgzms }}
      </a-descriptions-item>
      <a-descriptions-item label="发射通道号">
        {{ desData.fstdh ? desData.fstdh : '/' }}
      </a-descriptions-item>
    </a-descriptions>
  </a-card>
</template>

<script>
import { filterDictTextByCache } from '@/components/dict/JDictSelectUtil.js'
import { mapGetters } from 'vuex'
import { getAction } from '../../../api/manage'
export default {
  data() {
    return {
      formAntenna: {},
      labelCol: { span: 9 },
      wrapperCol: { span: 14 },
      desData: {},
      options: [
        { resKey: '电压', webKey: 'dy' },
        { resKey: '电流', webKey: 'dl' },
        { resKey: '温度', webKey: 'wd' },
        { resKey: '解调状态', webKey: 'jtzt' },
        { resKey: '下行通道1(国内)衰减器1', webKey: 'xxtd1_sjq1_gn' },
        { resKey: '下行通道1(国内)衰减器2', webKey: 'xxtd1_sjq2_gn' },
        { resKey: '下行通道1(国际)衰减器1', webKey: 'xxtd1_sjq1_gj' },
        { resKey: '下行通道1(国际)衰减器2', webKey: 'xxtd1_sjq2_gj' },
        { resKey: '上行通道衰减器1', webKey: 'sxtd_sjq1' },
        { resKey: '上行通道衰减器2', webKey: 'sxtd_sjq2' },
        { resKey: '本振锁定指示', webKey: 'bzsdzs' },
        { resKey: '设备状态', webKey: 'sbzt' },
        { resKey: '下行国内通道输入功率', webKey: 'xxgntdsrgl' },
        { resKey: '下行国际通道输入功率', webKey: 'xxgjtdsrgl' },
        { resKey: '设备地址', webKey: 'sbdz' },
        { resKey: '下行国内通道输入功率电压', webKey: 'xxgntdsrgldy' },
        { resKey: '下行国际通道输入功率电压', webKey: 'xxgjtdsrgldy' },
        { resKey: '导频信号开关', webKey: 'dpxhkg' },
        { resKey: '导频工作模式', webKey: 'dpgzms' },
        { resKey: '发射通道号', webKey: 'fstdh' },
        { resKey: '电流2', webKey: 'dl2' },
        { resKey: '温度2', webKey: 'wd2' },
        { resKey: '信号输入', webKey: 'xhsr' },
      ],
      url: {
        sendDevicePackage: '/device/nmcDeviceGridValue/sendDevicePackage',
        getByChannelCodeAndPackageCode: '/device/nmcDeviceGridValue/getByChannelCodeAndPackageCode',
        getByChannelCode: '/api/nmcAttr/getByChannelCode',
        list: 'nmc/activeInfo/list' //获取当前是主还是备
      },
      activeInfo: '',
      timer: null,
      title: '解调器状态显示'
    }
  },
  computed: {
    ...mapGetters(['websocketMsg']),
    websocketMsg() {
      return this.$store.state.user.websocketMsg
    }
  },
  watch: {
    websocketMsg: {
      handler(val) {
        console.log(9999, val)
        // 收到websocket的消息 则请求BUC-状态回报包,'===更换webSocket 的cmd'
        if (val) {
          let msg = JSON.parse(val)
          if (msg.cmd === 'qjt_sx') {
            this.getData('群解调')
          } else if (msg.cmd === 'qjt_sx_b') {
            this.getData('群解调备')
          }
        }
      }
    }
  },
  mounted() {
    // 'ANTENNA_REFRESH'
    // this.getData()
    this.getActiveInfo()
    this.startTimer()
  },
  destroyed() {
    this.stopTimer()
  },
  methods: {
    getData(type) {
      let str = ''
      if(this.activeInfo && type == '群解调') {
        str = '群解调'
      }else if(!this.activeInfo && type == '群解调备') {
        str = '群解调备'
      }
      if(!str) return 
      let temp = {
        channelCode: str
      }
      getAction(this.url.getByChannelCode, temp).then(res => {
        if (res.success) {
          res.result.forEach(i => {
            let obj = this.options.find(j => j.resKey == i.attrName)
            if (obj) {
              this.$set(this.desData, obj.webKey, i.parseValue)
            }
          })
        }
      })
    },
    getValue(res, label) {
      return res.result.find(i => i.packageGridCode == label).packageGridValue
    },
    getmapdata(res) {
      console.log(filterDictTextByCache('antenna_working_mode', '7'), '----')
      let temp = res.result
        .find(i => i.packageGridCode == '141-状态')
        .packageGridValue.toString()
        .split('')
      let data = parseInt(temp[1], 16)
        .toString(2)
        .padStart(4, 0)
      this.formAntenna.mode = filterDictTextByCache('antenna_working_mode', parseInt(temp[0], 16))
      this.$set(this.formAntenna, 'signalLockStatus', data[0])
      this.$set(this.formAntenna, 'inertialNavigationState', data[1])
      this.$set(this.formAntenna, 'beidouState', data[2])
      this.$set(this.formAntenna, 'currentAzimuthAngle', this.getValue(res, '141-当前方位角度'))
      this.$set(this.formAntenna, 'currentPitchAngle', this.getValue(res, '141-当前俯仰角度'))
      this.$set(this.formAntenna, 'targetAzimuthAngle', this.getValue(res, '141-目标方位角度'))
      this.$set(this.formAntenna, 'targetPitchAngle', this.getValue(res, '141-目标俯仰角度'))
      this.$set(this.formAntenna, 'DirectionalMotorSpeed', this.getValue(res, '141-方位电机速度'))
      this.$set(this.formAntenna, 'PitchMotorSpeed', this.getValue(res, '141-俯仰电机速度'))
      this.$set(this.formAntenna, 'InertialElevationAngle', this.getValue(res, '141-惯导俯仰角'))
      this.$set(this.formAntenna, 'InertialRollAngle', this.getValue(res, '141-惯导横滚角'))
      this.$set(this.formAntenna, 'InertialNavigationHeadingAngle', this.getValue(res, '141-惯导航向角'))
      this.$set(this.formAntenna, 'axisGyroscopeOfX', this.getValue(res, '141-X轴陀螺仪'))
      this.$set(this.formAntenna, 'axisGyroscopeOfY', this.getValue(res, '141-Y轴陀螺仪'))
      this.$set(this.formAntenna, 'axisGyroscopeOfZ', this.getValue(res, '141-Z轴陀螺仪'))
      this.$set(this.formAntenna, 'speed', this.getValue(res, '141-速度'))
      this.$set(this.formAntenna, 'longitude', this.getValue(res, '141-经度'))
      this.$set(this.formAntenna, 'latitude', this.getValue(res, '141-纬度'))
      this.$set(this.formAntenna, 'height', this.getValue(res, '141-高度'))
      this.$set(this.formAntenna, 'GPSSpeedE', this.getValue(res, '141-GPS东向速度'))
      this.$set(this.formAntenna, 'GPSSpeedN', this.getValue(res, '141-GPS北向速度'))
      this.$set(this.formAntenna, 'BDSXS', this.getValue(res, '141-北斗收星数'))
      this.$set(this.formAntenna, 'signalLevel', this.getValue(res, '141-信号电平'))
      this.$set(this.formAntenna, 'SignalToNoiseRatio', this.getValue(res, '141-信噪比'))
      this.$set(this.formAntenna, 'maximumCurrentSignalLevel', this.getValue(res, '141-当前信号电平最大值'))
      this.$set(this.formAntenna, 'AntennaAzimuthCorrectionValue', this.getValue(res, '141-天线方位修正值'))
      this.$set(this.formAntenna, 'AntennaPitchCorrectionValue', this.getValue(res, '141-天线俯仰修正值'))
      this.$set(this.formAntenna, 'SatelliteID', this.getValue(res, '141-卫星ID'))
      this.$set(this.formAntenna, 'SatelliteLongitude', this.getValue(res, '141-卫星经度'))
      this.$set(this.formAntenna, 'ReceivingFrequency', this.getValue(res, '141-接收频率'))
      this.$set(this.formAntenna, 'PolarizationMode', this.getValue(res, '141-接收极化方式'))
      this.$set(this.formAntenna, 'ZeroPositionSwitchStatus', this.getValue(res, '141-零位开关状态'))
      this.$set(this.formAntenna, 'FaultInformation', this.getValue(res, '141-故障信息'))
      this.$set(this.formAntenna, 'temperature', this.getValue(res, '141-温度'))
      let time = `${this.getValue(res, '141-年')}-${this.getValue(res, '141-月')}-${this.getValue(
        res,
        '141-日'
      )}  ${this.getValue(res, '141-时')}:${this.getValue(res, '141-分')}:${this.getValue(res, '141-秒')}`
      this.$set(this.formAntenna, 'time', time)
    },
    getStatus() {
      let data = {
        channelCode: '天线',
        packageCode: '141-天线-状态监控数据回报'
      }
      getAction(this.url.getByChannelCodeAndPackageCode, data).then(res => {
        if (res.success) {
          this.getmapdata(res)
        } else {
          this.$$message.error(res.message)
        }
      })
    },
    getActiveInfo() {
      getAction(this.url.list).then(res => {
        if (res.success) {
          let info = res.result.records[0]
          if(info && info.active) {
            this.activeInfo = info.active == '主' ? true : false
            this.title = `解调器状态显示（${info.active}）`
            this.getData()
          }
          // if (this.activeInfo) {
          //   this.getData('群解调')
          // } else {
          //   this.getData('群解调备')
          // }
        }
      })
    },
    startTimer() {
      if (this.timer) {
        clearInterval(this.timer)
      }
      this.timer = setInterval(() => {
        this.getActiveInfo()
      }, 1000)
    },
    stopTimer() {
      clearInterval(this.timer)
    },
  }
}
</script>

<style lang="less" scoped>
.ant-card-bordered {
  height: 100%;
  overflow: auto;
}

#JTQBox .ant-descriptions ::v-deep .ant-descriptions-title {
  font-size: 14px !important;
  line-height: 24px !important;
  margin-bottom: 5px !important;
}
</style>
